<?php
/*
+-----------------------------------------------------------------------------+
| $Id: *.php 2009-08-18 08:41:22Z Bleakwind $
| Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
| http://www.weaverdream.com/
+-----------------------------------------------------------------------------+
*/

if (!defined( 'ENTRY_INDEX')){
    echo "<h1>Forbidden</h1><p>You don't have permission to access on this server.</p>";
    exit;
}

// Admin group add and amend ajax
function submit_form($type, $value, $submit)
{
    global $LANGUAGE,$LANGLIST,$SETTING,$CONFIGURE,$MEMBER,$ADMIN,$db,$sys,$c,$setting_cover,$setting_attach,$sys,$ac;
    $ajax_response = new xajaxResponse();
    $error = false;
    //$ajax_response->alert(print_r($value, true)); $ajax_response->assign($submit,"disabled",false); return $ajax_response;

    $if_modipass    = false;
    $submit_value   = "编辑";

    if( !preg_match("/^[a-z0-9-_\.]+@([a-z0-9][a-z0-9-]*\.)+[a-z]{2,4}$/i",$value['email']) ) {
        $error = true;
        if(empty($anchor)) { $anchor = "email"; }
        $ajax_response->assign("email_return", "innerHTML", "<span class=\"prompt_failed\">信箱格式错误,请重新填写<!-- email error --></span>");
    } else {
        $ajax_response->assign("email_return", "innerHTML", "");
    }

    if($value['newpass1'] != "" || $value['newpass2'] != ""){
        if( func::str_encrypt($value['oldpass']) != $MEMBER['password'] ) {
            $error = true;
            if(empty($anchor)) { $anchor = "oldpass"; }
            $ajax_response->assign("oldpass_return", "innerHTML", "<span class=\"prompt_failed\">旧密码错误<!-- password same to oldpass --></span>");
        } else{
            $ajax_response->assign("oldpass_return", "innerHTML", "");
        }

        if( $value['newpass1'] != $value['newpass2'] ) {
            $error = true;
            if(empty($anchor)) { $anchor = "newpass1"; }
            $ajax_response->assign("newpass1_return", "innerHTML", "<span class=\"prompt_failed\">两次填写的密码不相同<!-- password diff --></span>");
            $ajax_response->assign("newpass2_return", "innerHTML", "<span class=\"prompt_failed\">两次填写的密码不相同<!-- password diff --></span>");
        } elseif( $value['oldpass'] == $value['newpass1'] ) {
            $error = true;
            if(empty($anchor)) { $anchor = "newpass1"; }
            $ajax_response->assign("newpass1_return", "innerHTML", "<span class=\"prompt_failed\">修改密码时新密码不能与旧密码相同<!-- password same to oldpass --></span>");
            $ajax_response->assign("newpass2_return", "innerHTML", "<span class=\"prompt_failed\">修改密码时新密码不能与旧密码相同<!-- password diff --></span>");
        } else{
            $ajax_response->assign("newpass1_return", "innerHTML", "");
            $ajax_response->assign("newpass2_return", "innerHTML", "");
        }
        
        if(!$error){
            $if_modipass = true;
            $sql_password = "password='".func::str_encrypt($value['newpass1'])."',";
        }
    }

    if($error){
        $ajax_response->assign($submit,"value",$submit_value);
        $ajax_response->assign($submit,"disabled",false);
        $ajax_response->redirect("#".$anchor);
    }else{

        $sql = "UPDATE ".DB_TABLE_MEMBER." SET
                    ".$sql_password."
                    email       = '".$value['email']."',
                    ip_change   = '".func::return_ip()."',
                    time_change = ".$sys->nowtime."
                WHERE id=".$ADMIN['mid'];
        $result = $db->Execute($sql);
        if (!$result) {
            $ajax_response->alert($db->ErrorMsg());
            $ajax_response->assign($submit,"value",$submit_value);
            $ajax_response->assign($submit,"disabled",false);
        }else{
            $sql = "UPDATE ".DB_TABLE_ADMIN." SET
                        ip_change   = '".func::return_ip()."',
                        time_change = ".$sys->nowtime."
                    WHERE mid=".$ADMIN['mid'];
            $result = $db->Execute($sql);
            if (!$result) {
                $ajax_response->alert($db->ErrorMsg());
                $ajax_response->assign($submit,"value",$submit_value);
                $ajax_response->assign($submit,"disabled",false);
            }

            if($if_modipass){
                $ac->destroy();
                $ajax_response->redirect($CONFIGURE['common']['control_admin']."?act=login");
            }else{
                $sys->set_prompt(array(
                    "prompt_type"       => "'prompt'",
                    "prompt_if"         => "1",
                    "prompt_message"    => "'修改资料成功'",
                    "prompt_returnurl"  => "'".$CONFIGURE['common']['control_admin']."?act=index'",
                    "prompt_returntext" => "'返回首页'",
                    "prompt_image"      => "'succeed.gif'",
                    "prompt_act"        => "'prompt'",
                ));
                $ajax_response->redirect($CONFIGURE['common']['control_admin']."?act=prompt");
            }
        }
    }

    return $ajax_response;
}
$bwajax->register(XAJAX_FUNCTION, "submit_form");
//////
?>
